package common.web.action;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import util.ImageCodeUtil;
import common.web.WebHelper;

public class ImageCodeAction {
	public void getImageCode(WebHelper webHelper) throws IOException {		
		HttpServletRequest request = webHelper.getRequest();
		HttpServletResponse response = webHelper.getResponse();
		HttpSession session = request.getSession();
		
		response.setContentType("image/jpeg");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0L);
		ImageCodeUtil image = new ImageCodeUtil();
		try {
			BufferedImage img = image.creatImage();
			session.setAttribute("imageCode",image.getSRand());
			ImageIO.write(img, "JPEG", response.getOutputStream());
			
			response.getOutputStream().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			response.getOutputStream().close();
		}
	}
	
}
